#/usr/bin/env python
# -*- coding: UTF-8 -*-
import requests
from bs4 import BeautifulSoup
import urllib
import os
#url ="http://gwxlm.15166.com/"
url="http://gwxlm.15166.com/" #爬取网站url
root ="E:/pytest/" #保存的图片路径
path = root+url
wbdata = requests.get(url).text
soup = BeautifulSoup(wbdata,'html.parser')
img= soup.find_all("img")
for i in img:
	imgsrc=''
	getsrc=i.get('data-src')
	if not getsrc:
		imgsrc=i.get("src")
	else:
		imgsrc=getsrc
	#根据网站图片路径进行匹配
	imgurl="http:"+imgsrc
	imgtype=imgurl.split('/')[-1]
	imgtype=imgtype.split('?')[0]
	path="E:\pytest\img\\"+imgtype
	if os.path.exists(path):
		print('----文件已存在------'.decode("utf-8").encode("gbk"))#处理中文乱码
	else:
		pic_file = urllib.urlopen(imgurl).read()
		f = open(path,"wb")
		f.write(pic_file)
		f.close()
	